package highTech.Thread.shareThreadData;

public class MutilThreadShareData {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/** ��ʽһ **/
		ShareData data=new ShareData();
		new Thread(new MyRunnable1(data)).start();
		new Thread(new MyRunnable2(data)).start();
		/** ��ʽ�� **/
		final ShareData data1=new ShareData();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				data1.increment();
				
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				data1.decrement();
				
			}
		}).start();
		
	}
}

class MyRunnable1 implements Runnable{
	private ShareData data;
	public MyRunnable1(ShareData data){
		this.data=data;
	}
	@Override
	public void run() {
		data.increment();
	}
}

class MyRunnable2 implements Runnable{
	private ShareData data;
	public MyRunnable2(ShareData data){
		this.data=data;
	}
	@Override
	public void run() {
		data.decrement();
	}
}

class ShareData{
	private int i=0;
	public synchronized void increment(){
		i++;
	}
	
	public synchronized void decrement(){
		i--;
	}
	
}